<?php
/*
 * 快递添加
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\Express;


class ExpressController extends BaseController{

    //快递列表
    public function expressList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $express_name = Request::param('express_name');
        $w_name = Request::param('w_name');
//        $type = Request::param('type');

        $where = [];
        $where[] = ['e.enable','=','1'];
        if(!empty($express_name)){
            $where[] = ['e.express_name','=',$express_name];
        }
        
        if(!empty($w_name)){
            $where[] = ['w.w_name','=',$w_name];
        }
//        if(!empty($type)){
//            $where[] = ['type','=',$type];
//        }
        $list = Express::expressList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Express::expressCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //快递添加
    public function expressAdd(){
        //$store = new Store();
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $express_name = Request::param('express_name');
        $unit_weight_min = Request::param('unit_weight_min');
        $unit_weight_max = Request::param('unit_weight_max');
        $unit = Request::param('unit');
        $price = Request::param('price');
        $w_id = Request::param('w_id');
        
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        $time = time();
        $data = [
            'express_name' => $express_name,
            'unit_weight_min' => $unit_weight_min,
            'unit_weight_max' => $unit_weight_max,
            'unit' => $unit,
            'price' => $price,
            'w_id' => $w_id,
            'enable' => $enable,
            'addtime' => $time,
        ];

        Express::expressAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //快递详情
    public function expressInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = Express::expressInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);

            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //快递修改
    public function expressEdit(){

        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $enable = Request::param('enable');
        $express_name = Request::param('express_name');
        $unit_weight_min = Request::param('unit_weight_min');
        $unit_weight_max = Request::param('unit_weight_max');
        $unit = Request::param('unit');
        $price = Request::param('price');
        $w_id = Request::param('w_id');
        
        $update = [];
        if(!empty($express_name)){
            $update['express_name'] = $express_name;
        }
        
        if(!empty($unit_weight_min)){
            $update['unit_weight_min'] = $unit_weight_min;
        }
        
        if(!empty($unit_weight_max)){
            $update['unit_weight_max'] = $unit_weight_max;
        }
        
        if(!empty($unit)){
            $update['unit'] = $unit;
        }
        
        if(!empty($price)){
            $update['price'] = $price;
        }
        
        if(!empty($w_id)){
            $update['w_id'] = $w_id;
        }
        
        if($enable==-1){
            $update['enable'] = 0;
        }else{
            $update['enable'] = 1;
        }


        Express::expressEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

}
